#  -*- coding: utf-8 -*-
#  @Time    : 2021/10/26 14:17
#  @Author  : super

import socket

if __name__ == '__main__':

	# 创建socket链接
	tcp_socket_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

	# 设置端口号服用
	tcp_socket_server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)

	# 指定地址、端口
	tcp_socket_server.bind(("", 9091))

	# 创建链接请求数
	tcp_socket_server.listen(128)

	# 等待客户建立链接
	new_socket_client, ip_ropt = tcp_socket_server.accept()

	# 客户端给服务端发送数据
	recv = new_socket_client.recv(4096)
	print(recv.decode("gbk"))

	# 给客户端发送数据
	with open("static/index.html", "r") as file:
		file_data = file.read()
		
	# 响应行
	response_lines = "HTTP/1.1 200 OK\r\n"
	# 响应头
	response_header = "Server: BWS/1.0 www.baidu.com\r\n"
	# 响应体
	response_body = file_data
	
	# 封装到一起
	response = response_lines+ response_header + "\r\n" + response_body
	new_socket_client.send(response.encode("gbk"))

	# 关闭客户端链接
	new_socket_client.close()